package com.yayo.mangement.facade.impl;

import com.yayo.mangement.converter.SysUserRoleConverter;
import com.yayo.mangement.facade.SysUserRoleFacade;
import com.yayo.mangement.model.SysUserRoleDO;
import com.yayo.mangement.dto.SysUserRoleDTO;
import com.yayo.mangement.service.SysUserRoleService;
import com.yayo.mangement.paging.Paging;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.yayo.mangement.response.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 
 * @author: Yayo
 * @date: 2020-08-07 14:18:40
 */
@Service
public class SysUserRoleFacadeImpl implements SysUserRoleFacade{

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysUserRoleConverter sysUserRoleConverter;

    @Override
    public Response<Paging<SysUserRoleDTO>> paging(Integer pageNo, Integer pageSize,Map<String,Object> params){
        Paging<SysUserRoleDO> sysUserRoleDOPaing = sysUserRoleService.paging(pageNo,pageSize,params);
        List<SysUserRoleDTO> sysUserRoleDTOList = sysUserRoleDOPaing.getData().stream().map(sysUserRoleConverter::DO2DTO).collect(Collectors.toList());
        return Response.ok(new Paging<SysUserRoleDTO>(sysUserRoleDOPaing.getTotal(),sysUserRoleDTOList));
    }

    @Override
    public Response<SysUserRoleDTO> detail(Long id){
        SysUserRoleDO sysUserRoleDO = sysUserRoleService.findById(id);
        return Response.ok(sysUserRoleConverter.DO2DTO(sysUserRoleDO));
    }

    @Override
    public Response create(SysUserRoleDTO sysUserRoleDTO){
        SysUserRoleDO sysUserRoleDO = sysUserRoleConverter.DTO2DO(sysUserRoleDTO);
        Boolean isTrue = sysUserRoleService.create(sysUserRoleDO);
        if(isTrue){
            return Response.ok();
        }
        return Response.fail("create.data.error");
    }

    @Override
    public Response update(SysUserRoleDTO sysUserRoleDTO){
        SysUserRoleDO sysUserRoleDO = sysUserRoleConverter.DTO2DO(sysUserRoleDTO);
        Boolean isTrue = sysUserRoleService.updateById(sysUserRoleDO);
        if(isTrue){
            return Response.ok();
        }
        return Response.fail("update.date.error");
    }

    @Override
    public Response delete(Long id){
        boolean isTrue = sysUserRoleService.delete(id);
        if(isTrue){
            return Response.ok();
        }
        return Response.fail("delete.date.error");
    }

}